<?php
class RecommendationControl extends CI_Controller
{
	function __construct() 
	{		
		parent::__construct();
		$this->load->model('similargenremodel');
		$this->load->model('moviesmodel');
		$this->load->model('genredominanmodel');
		$this->load->model('ratingsmodel');
		$this->load->model('plotsmodel');
		$this->load->model('producersmodel');
		$this->load->model('actorsmodel');
		$this->load->model('movies2actorsmodel');
		$this->load->model('movies2producersmodel');
		$this->load->model('genresmodel');
		$this->load->model('rekomendasiModel');
		$this->load->model('usiaUserModel');
		$this->load->library('session');
	}
	
	function recommendation(){
		$session_data = $this->session->userdata('logged_in');
     	$userid = $session_data['userid'];
     	$username = $session_data['username'];
     	$gender = $session_data['gender'];
     	$usia = $this->usiaUserModel->getUsia($userid)->result();
		if (count($usia) > 1) {
     		for ($j2 = 0; $j2 < count($usia); $j2++) {
     			$age[$j2] = $usia[$j2]->USIAID;
     		}
     		$usiaid = max($age);
     	} else {
     		$usiaid = $usia[0]->USIAID;
     	}
		$genreid = $this->similargenremodel->getSimilarGenre($userid, $usiaid)->result(); //2 = userid, 1 = usiaid
		$genre = $this->genredominanmodel->getGenre($genreid[0]->GENREID)->result();
		$movie = $this->moviesmodel->getMovieDominan($genreid[0]->GENREID, $userid)->result();
		if (count($movie) == null) {
			$movie = $this->moviesmodel->getMovie($genre[0]->GENREDOM, $userid)->result();
		}
		if (count($movie) > 0){	
			$i = 0;
			$reco[$i]['detail'] = $this->moviesmodel->getDataMovie($movie[$i]->MOVIEID)->result();
			$reco[$i]['plot'] = $this->plotsmodel->getPlot($movie[$i]->MOVIEID)->result();
			$actor[$i] = $this->movies2actorsmodel->getActorId($movie[$i]->MOVIEID)->result();
			$producer[$i] = $this->movies2producersmodel->getProducerId($movie[$i]->MOVIEID)->result();
			$reco[$i]['genre'] = $this->genresmodel->getGenre($movie[$i]->MOVIEID)->result();
			for ($j = 0; $j < count($actor); $j++) {
				$reco[$i]['actor'][$j] = $this->actorsmodel->getActorName($actor[$j][0]->ACTORID)->result();
			}
			for ($k = 0; $k < count($producer); $k++) {
				$reco[$i]['producer'][$k] = $this->producersmodel->getProducerName($producer[$k][0]->PRODUCERID)->result();
			}
			$this->load->view('recommendationpage',$reco[$i]);
			$this->nextRecomendation($userid, $movie[$i]->MOVIEID);
		} else {
			$reco['detail'] = null;
			$reco['notif'] = "Belum ada rekomendasi film lagi untuk anda";
			$this->load->view('recommendationpage',$reco);
		}
	}
	
	
	function nextRecomendation($userid, $movieid){
		if ($this->input->post('next')) {
			$this->rekomendasiModel->saveRecommendation($userid, $movieid);
			redirect('recommendationcontrol/recommendation', 'refresh');
		}
	}
	}
?>